package com.mvc.controller;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.omg.CORBA.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;

import com.db.entity.LoginProxy;
import com.db.entity.User;

@Controller
@RequestMapping("/user")
public class LoginController {
	private static Logger log = Logger.getLogger(LoginController.class); 
	
	@RequestMapping({"/test","/"})
	public String test(@RequestParam("username") String username){
		log.debug("test called");
		
		return "test";
	}
	
	@RequestMapping({"/test2","/"})//如果
	public String test2(@RequestParam("username")String username, Map<String,Object> context) {
		log.debug("test called!");
		
		return "test2";
	}
	
	@RequestMapping({"/test3","/"})
	public String test3(){
		return "";
	}
	
	@RequestMapping(value="/doLogin3", method=RequestMethod.POST)
	public String doLogin3(@ModelAttribute User user, Model model){
		log.debug("doLogin called");
		//Map map = (Map) model.asMap();
		model.addAttribute("xxx","xxx22");
		log.debug("id:" + user.getUserid());
		log.debug("name:"+user.getUsername());
		log.debug("pass:"+user.getPassword());
		return "login3";
	}
	
	@RequestMapping(value="/doLogin3", method=RequestMethod.GET)
	public String doLogin3(Model model){
		log.debug("doLogin called");
		//Map map = (Map) model.asMap();
		model.addAttribute("xxx","xxx11");
		List<String> list = new LinkedList<String>();
		list.add("江苏");
		list.add("浙江");
		model.addAttribute("pro", list);
		return "login3";
	}
	
	@RequestMapping(value="/doLogin", method=RequestMethod.GET)
	public String doLogin(Model model){
		log.debug("doLogin2 get called");
		model.addAttribute("loginProxy",new LoginProxy());
		return "login";
	}
	
	@RequestMapping(value="/doLogin", method=RequestMethod.POST)
	public String doLogin(@ModelAttribute LoginProxy user, Model model, HttpSession session){
		
		log.debug("doLogin2 post called");
		log.debug(user.getUsername());
		log.debug(user.getPassword());
		model.addAttribute("showError", true);
		model.addAttribute("loginProxy", user);
		session.setAttribute("username", "username");
		return "forward:/wh/write.do";
	}
}
